import {message} from "antd";
import {randomId} from "./common";

export const createData = (key,data) => {
    const stringData = JSON.stringify(data)
    window.localStorage.setItem(key,stringData)
}
export const getData = (key) => {
    const stringData = window.localStorage.getItem(key)
    return stringData ? JSON.parse(stringData) : message.error('数据不存在')
}
export const addItemData = (key, newData) => {
    const oldArrData = JSON.parse(window.localStorage.getItem(key))
    // if (!newData.id) newData.id = randomId()
    window.localStorage.setItem(key,JSON.stringify([...oldArrData, newData]))
}
export  const delItemData = (key, id) => {
    const oldArrData = JSON.parse(window.localStorage.getItem(key))
    const newArrData = oldArrData.filter(item => {
        return item.id !== id
    })
    window.localStorage.setItem(key,JSON.stringify(newArrData))
}
export const removeData = (key) => {
    window.localStorage.removeItem(key)
}

export const changeData = (key, data) => {
    window.localStorage.setItem(key,JSON.stringify(data))
}
